/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.sesame.service.AlbumService;

/**
 * @author voidman
 * 
 */

public class DeleteCommentController extends AbstractController {

    private AlbumService albumService;

    public void setAlbumService(AlbumService albumService) {
        this.albumService = albumService;
    }

    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse reponse) throws Exception {

        Integer commentId = Integer.parseInt(request.getParameter("commentId"));
        Integer imageId = Integer.parseInt(request.getParameter("imageId"));
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        Integer imageUserId = albumService.showImage(imageId).getUserId();

        if (userId.intValue() == imageUserId.intValue()) {
            albumService.deleteComment(commentId);
            return new ModelAndView("redirect:imageDetail.html?imageId="
                    + imageId);
        } else {
            return new ModelAndView("redirect:error.html");
        }

    }

}